<template>
  <FModal :loading="loading" ref="refFModal" @ok="ok" width="500" header="编辑">
    <FPage>
      <template #header>
        <el-form style="padding-right: 30px; padding-top: 10px"
                 ref="refForm" status-icon label-width="120px" :model="dict">
          <el-form-item prop="dictCode" label="编码" :rules="required">
            <el-input v-model="dict.dictCode" placeholder="请输入编码"/>
          </el-form-item>
          <el-form-item prop="dictName" label="名称" :rules="required">
            <el-input v-model="dict.dictName" placeholder="请输入名称"/>
          </el-form-item>
        </el-form>
      </template>
    </FPage>
  </FModal>
</template>
<script setup>
import FModal from '@/components/FModal/index.vue';
import {nextTick, ref, shallowRef} from 'vue';
import FPage from '@/components/layout/FPage.vue';
import {real, required} from '@/utils/CommonUtils';
import {putAction} from '@/request/BaseRequest';

const loading = shallowRef();
const refForm = shallowRef();
const refFModal = shallowRef();
const dict = ref({});
const emit = defineEmits(['ok']);

function open() {
  refFModal.value.open();
}

function addNew() {
  open()
  nextTick(() => {
    dict.value = {}
    refForm.value.resetFields();
  })
}

function setData(data) {
  dict.value = data;
  open();
}

function ok() {
  refForm.value.validate((valid, errors) => {
    if(valid) {
      save();
    }
  })
}

function save() {
  loading.value = true;
  putAction('/plat/platDict/save', real(dict)).then(data => {
    loading.value = false;
    if (data.success) {
      let result = data.result;
      close()
      emit('ok')
    }
  })
}

function close() {
  refFModal.value.close();
}

defineExpose({
  setData,
  addNew
});

</script>
<script>
export default {
  name: 'SysDictEditor'
};
</script>
<style scoped>

</style>